package com.bdqn.t350.vue.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.t350.vue.bean.LoginUser;
import com.bdqn.t350.vue.bean.User;
import com.bdqn.t350.vue.mapper.UserMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {


    @Resource
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String usrName) throws UsernameNotFoundException {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("usr_name", usrName);
        User user = userMapper.selectOne(queryWrapper);
        //去数据库里面找用户
        if (Objects.isNull(user)) {
            throw new RuntimeException("用户名密码错误!!!");
        }
        //通过用户去数据库中查询改用户的权限
        //查询该用户的权限信息封装到UserDetails
        List<String> list = new ArrayList<>(Arrays.asList("test"));
        return new LoginUser(user, list);
    }
}
